﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class IndustryController : BaseController
    {
        #region 列表
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            int order;
            int.TryParse(Request["order[0][column]"], out order);
            var direction = Request["order[0][dir]"];
            var orderByColumns = new[] { "Name", "ParentId" };
            var search = Request["search[value]"];
            var pageIndex = start/length + 1;
            var theList = IndustryBLL.Instance.GetPagingList(draw, pageIndex, length, SqlSafe(search), orderByColumns[order] + " " + direction);
            return Json(theList);
        }
        #endregion

        #region 添加
        public ActionResult Create()
        {
            InitiSet(0);
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, Industry model)
        {
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var result = IndustryBLL.Instance.Add(model);
            if (result <= 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
            return RedirectToAction("List", "Industry");
        }
        #endregion

        #region 修改
        public ActionResult Update(int id)
        {
            InitiSet(id);
            var model = IndustryBLL.Instance.GetModel(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(FormCollection collection, Industry model)
        {
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var result = IndustryBLL.Instance.Update(model);
            if (!result)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
            return RedirectToAction("List", "Industry");
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Delete()
        {
            var ids = Request["doIds"];
            var result = IndustryBLL.Instance.DeleteList(ids);
            return Json(result);
        }
        #endregion

        /// <summary>
        /// 根据父级ID获取行业列表
        /// </summary>
        public JsonResult GetIndustryByParentId(int parentId)
        {
            return Json(IndustryBLL.Instance.GetModelList("ParentId=" + parentId));
        }

        #region 自用函数
           [Matrix.MaKaYunShang.Model.Attributes.Skip]
        private void InitiSet(int id)
        {
            var parentIndustryList = IndustryBLL.Instance.GetModelList("ParentId=0 and Id<>" + id).Select(model => new SelectListItem
            {
                Value = model.Id.ToString(),
                Text = model.Name
            }).ToList();
            parentIndustryList.Insert(0, new SelectListItem { Value = "0", Text = "无" });
            ViewBag.ParentIndustryList = parentIndustryList;
        }
        #endregion
    }
}